<template>
  <el-select size="mini" v-model="selDepartment" placeholder="部门" @change="selEvent">
    <el-option v-for="department in departmentList" :key="department.id" :label="department.name" v-model="department.id"></el-option>
  </el-select>
</template>

<script>
  import errHandler from '@/assets/errHandler'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  export default {
    name: "selDepartment",
    props:['departmentId','companyId'],
    data() {
      return {
        departmentList:null,
        selDepartment:null
      }
    },
    watch:{
      companyId(nv,ov){
        if(nv) this.init(nv)
      }
    },
    created(){
      this.init(this.companyId)
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      init(companyId){
        console.log('能看到公司么？？？？？？',companyId)
        this.selDepartment=this.departmentId
        this.getDepartmentList(companyId)
      },
      getDepartmentList(companyId){
        this.fetch({name:"getDepartmentList",data:{companyId}})
          .then(res=>{
            this.departmentList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      selEvent(e){
        console.log('选部门---------',e)
        this.$emit('confirm',e)
      }
    }
  }
</script>

<style scoped>

</style>
